package ch.inputRoute;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PathfinderDbAdapter {
	 private final Context mCtx;
	 private DatabaseHelper mDbHelper;
	 private SQLiteDatabase mDb;
	 
	 public final String KEY_COORDINATE1 = "coordinate1";
	 public final String KEY_COORDINATE2 = "coordinate2";
	 public final String KEY_DESCRIPTION = "description";
	 public final String KEY_ROWID = "_id";
	 
	 public PathfinderDbAdapter(Context ctx) {
	     this.mCtx = ctx;
	 }
	 
	 /**
	     * Open the pathfinder database. If it cannot be opened, try to create a new
	     * instance of the database. If it cannot be created, throw an exception to
	     * signal the failure
	     * 
	     * @return this (self reference, allowing this to be chained in an
	     *         initialization call)
	     * @throws SQLException if the database could be neither opened or created
	     */
	 public PathfinderDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
     }

     public void close() {
         mDbHelper.close();
     }
     
     /**
      * Create a new place using the coordinate1, coordinate2 and description. If the place is
      * successfully created return the new rowId for that place, otherwise return
      * a -1 to indicate failure.
      * 
      * @param coordinate1 the first coordinate of the place
      * @param coordinate2 the second coordinate of the place
      * @param description the description of the place
      * @return rowId or -1 if failed
      */
     public long createPlace(float coordinate1, float coordinate2, String description) {
         ContentValues initialValues = new ContentValues();
         initialValues.put(KEY_COORDINATE1, coordinate1);
         initialValues.put(KEY_COORDINATE2, coordinate2);
         initialValues.put(KEY_DESCRIPTION, description);

         return mDb.insert(DatabaseHelper.DATABASE_TABLE, null, initialValues);
     }
     
     /**
      * Delete the place with the given rowId
      * 
      * @param rowId id of place to delete
      * @return true if deleted, false otherwise
      */
     public boolean deletePlace(long rowId) {
         return mDb.delete(DatabaseHelper.DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
     }
     
     public boolean deletePlaces() {
         return mDb.delete(DatabaseHelper.DATABASE_TABLE, null, null) > 0;
     }
     
     /**
      * Returns a Cursor over the list of all places
      * 
      * @return Cursor over all places
      */
     public Cursor GetAllPlaces() {

         return mDb.query(DatabaseHelper.DATABASE_TABLE, new String[] {KEY_ROWID, KEY_COORDINATE1, KEY_COORDINATE2, KEY_DESCRIPTION}, null, null, null, null, null);
     }
     
     /**
      * Return a Cursor positioned at the place that matches the given rowId
      * 
      * @param rowId id of place to retrieve
      * @return Cursor positioned to matching place, if found
      * @throws SQLException if place could not be found/retrieved
      */
     public Cursor fetchPlace(long rowId) throws SQLException {

         Cursor mCursor =

             mDb.query(true, DatabaseHelper.DATABASE_TABLE, new String[] {KEY_ROWID,
                     KEY_COORDINATE1, KEY_COORDINATE2, KEY_DESCRIPTION}, KEY_ROWID + "=" + rowId, null,
                     null, null, null, null);
         if (mCursor != null) {
             mCursor.moveToFirst();
         }
         return mCursor;

     }
     
     /**
      * Update the place using the details provided. The place to be updated is
      * specified using the rowId, coordinate1, coordinate2 and description
      * 
      * @param rowId id of place to update
      * @param coordinate1 value to set first coordinate
      * @param coordinate2 value to set second coordidnate
      * @param description value to set description of the place
      * @return true if the place was successfully updated, false otherwise
      */
     public boolean updatePlace(long rowId, float coordinate1, float coordinate2, String description) {
         ContentValues args = new ContentValues();
         args.put(KEY_COORDINATE1, coordinate1);
         args.put(KEY_COORDINATE2, coordinate2);
         args.put(KEY_DESCRIPTION, description);

         return mDb.update(DatabaseHelper.DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
     }
}